package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;

@Actions
@Namespace("/")
@ParentPackage("struts-default")

@Controller
@Scope("prototype")
public class AreaAction extends ActionSupport implements ModelDriven<Area> {

	@Autowired
	private AreaService areaService;
	
	private Area a = new Area();

	@Override
	public Area getModel() {
		return a;
	}
	
	private File file;
	private String fileFileName;	//用来接收文件的名称

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Action(value = "area_batchImport")
	public String restoreBatch() throws IOException {
		List<Area> list = new ArrayList<Area>();
		System.out.println(file);
		System.out.println(fileFileName);
		//1、加载Excel文件对象
		Workbook workbook = null;
		if(fileFileName.endsWith(".xls")) {
			workbook = new HSSFWorkbook(new FileInputStream(file));
		}else {
			workbook = new XSSFWorkbook(new FileInputStream(file));
		}
		//2、读取一个sheet
		Sheet sheet = workbook.getSheetAt(0);
		
		for (Row row : sheet) {
			//一行数据 对应一个区域对象
			if(row.getRowNum() == 0) {
				//跳过表头
				continue;
			}
			//跳过空行
			if(row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			list.add(area);
		}
		
		areaService.saveBatch(list);
		workbook.close();
		return NONE;
	}
	
	
	/*public String restoreBatch() throws IOException {
		List<Area> list = new ArrayList<Area>();
		System.out.println(file);
		System.out.println(fileFileName);
		//1、加载Excel文件对象
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		//2、读取一个sheet
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		
		for (Row row : sheet) {
			//一行数据 对应一个区域对象
			if(row.getRowNum() == 0) {
				//跳过表头
				continue;
			}
			//跳过空行
			if(row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			list.add(area);
		}
		
		areaService.saveBatch(list);
		
		
		XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(file));
		
		return NONE;
	}*/

}
